# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=headers
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgdesc="MinGW-w64 headers for Windows"
pkgver=9.0.0.6166.0a00d0ea
pkgrel=2
_commit='0a00d0ea9b2cff3f22b276201fd22daee162ff07'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
makedepends=('git' "${MINGW_PACKAGE_PREFIX}-tools")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' '!libtool' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit"
        0001-Allow-to-use-bessel-and-complex-functions-without-un.patch
        0002-DirectX-9-fixes-for-VLC.patch
        0003-crt-stdlib-Add-quick_exit-and-at_quick_exit-for-UCRT.patch)
sha256sums=('SKIP'
            'd641257f7e1469aff89adc33e57702b75fe9667ca035978f890eae1020b6814c'
            '09b1c7b62f666a07609af57e10c2b0ad815b78356f4b0f1fb6d827a1109a0ec7'
            'c9d357aa43cf7a2c01c2936d7e323a5d20e9f1f743e87db84d1fefe305e5e216')

pkgver() {
  cd "${srcdir}/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

prepare() {
  cd ${srcdir}/mingw-w64

  git am --committer-date-is-author-date "${srcdir}/0001-Allow-to-use-bessel-and-complex-functions-without-un.patch"
  git am --committer-date-is-author-date "${srcdir}/0002-DirectX-9-fixes-for-VLC.patch"

  # https://github.com/msys2/MINGW-packages/issues/8425#issuecomment-826046147
  git apply -R "${srcdir}/0003-crt-stdlib-Add-quick_exit-and-at_quick_exit-for-UCRT.patch"

  cd ${srcdir}/mingw-w64/mingw-w64-headers
  touch include/windows.*.h include/wincrypt.h include/prsht.h
}

build() {
  local _default_win32_winnt
  case "${CARCH}" in
    i686|x86_64)
      _default_win32_winnt=0x601
      ;;
    *)
      # assume any new arches added will be at least Win10
      _default_win32_winnt=0xA00
      ;;
  esac

  local _default_msvcrt=msvcrt
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _default_msvcrt=ucrt
  fi

  msg "Configuring ${MINGW_CHOST} headers"
  [[ -d ${srcdir}/headers-${MINGW_CHOST} ]] && rm -rf ${srcdir}/headers-${MINGW_CHOST}
  mkdir -p ${srcdir}/headers-${MINGW_CHOST} && cd ${srcdir}/headers-${MINGW_CHOST}
  ${srcdir}/mingw-w64/mingw-w64-headers/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX}/${MINGW_CHOST} \
    --enable-sdk=all \
    --with-default-win32-winnt=${_default_win32_winnt} \
    --with-default-msvcrt=${_default_msvcrt} \
    --enable-idl \
    --without-widl
}

package() {
  msg "Installing ${MINGW_CHOST} headers"
  cd ${srcdir}/headers-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  rm ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/pthread_signal.h
  rm ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/pthread_time.h
  rm ${pkgdir}${MINGW_PREFIX}/${MINGW_CHOST}/include/pthread_unistd.h

  msg "Installing MinGW-w64 licenses"
  install -Dm644 ${srcdir}/mingw-w64/mingw-w64-headers/ddk/readme.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ddk-readme.txt
  install -Dm644 ${srcdir}/mingw-w64/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 ${srcdir}/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
